﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// getData 获得数据的函数
/// </summary>
public class getData
{
    public getData()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    public static  void addNewsVisit(string newsid)
    {
        SqlDataSource ds = new SqlDataSource();
        ds.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Jxzy_Database"].ToString();
        ds.UpdateCommand = "update zyk_News set News_Visit=News_Visit+1 where News_ID=@newsid";
        ds.UpdateParameters.Add("newsid", TypeCode.Int32, newsid);
        ds.Update();
        ds.Dispose();
    }
    public static  void addResourceVisit(string resourceid)
    {
        SqlDataSource ds = new SqlDataSource();
        ds.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Jxzy_Database"].ToString();
        ds.UpdateCommand = "update zyk_Resources set Resource_Visit=Resource_Visit+1 where Resource_ID=@resourceid";
        ds.UpdateParameters.Add("resourceid", TypeCode.Int32, resourceid);
        ds.Update();
        ds.Dispose();
    }
    /// <summary>
    /// 获得时间格式
    /// </summary>
    /// <param name="iDateType"></param>
    /// <param name="sDate"></param>
    /// <returns></returns>
    private static string dateFormat(int iDateType, string sDate)
    {
        DateTime a = Convert.ToDateTime(sDate);
        string sReturn = "";
        switch (iDateType)
        {
            case 1:
                sReturn = a.ToString("yyyy/MM/dd");
                break;
            case 2:
                sReturn = a.ToString("yyyy-MM-dd");
                break;
            case 3:
                sReturn = a.ToString("yyyy年MM月dd日");
                break;
            default:
                sReturn = a.ToString("yyyy-MM-dd");
                break;
        }
        return sReturn;
        
    }
    /// <summary>
    /// 获得文章列表
    /// </summary>
    /// <param name="sMajorID">专业代码</param>
    /// <param name="iOrderType">排序类型,1为时间,2为点击</param>
    /// <param name="iType">分类型,新闻或者资源里面的类型名称</param>
    /// <param name="iCount">读取的记录条数</param>
    /// <param name="iTitleLen">标题长度</param>
    /// <returns><li><a href="#" title="sherry12312">sherry</a></li></returns>
    public static string getList(string sMajorID,int iDataType,int iOrderType,int iType, int iCount, int iTitleLen)
    {
        DataView dv;
        string sReturn="";
        dv = getView(sMajorID,iDataType, iOrderType, iType, iCount, iTitleLen);
        if (dv.Count > 0)
        {
            for (int i = 0; i < dv.Count; i++)
            {
                if (iDataType == 1)
                {
                    sReturn += "<li><a href='NewsShow.aspx?newsid=" + dv[i]["News_ID"].ToString() + "' title='" + dv[i]["fulltitle"].ToString() + "'>" + dv[i]["title"] + "</a></li>\r\n";
                }
                else
                {
                    sReturn += "<li><a href='ResourceShow.aspx?rsid=" + dv[i]["Resource_ID"].ToString() + "' title='" + dv[i]["fulltitle"].ToString() + "'>" + dv[i]["title"] + "</a></li>\r\n";
                }
            }
        }
        else
        {
            sReturn = "<li>没有找到记录</li>";
        }
        
        return sReturn;

    }
    /// <summary>
    /// 获得文章列表重载
    /// </summary>
    /// <param name="sMajorID">专业代码</param>
    /// <param name="iOrderType">排序类型,1为时间,2为点击</param>
    /// <param name="iType">分类型,新闻或者资源里面的类型名称</param>
    /// <param name="iCount">读取的记录条数</param>
    /// <param name="iTitleLen">标题长度</param>
    /// <param name="sDateClass">日期类名</param>
    /// <param name="iDateType">日期类型1: 2009/1/1 ,2:2009-1-1,3:2009年1月1日</param>
    /// <returns><li><a href="#" title="aa">sherry</a><span class="dateclass">2009-01-01</span></li></returns>
    public static string getList(string sMajorID,int iDataType, int iOrderType, int iType, int iCount, int iTitleLen, string sDateClass, int iDateType)
    {
        DataView dv;
        string sReturn = "";
        dv = getView(sMajorID,iDataType, iOrderType, iType, iCount, iTitleLen);
        if (dv.Count > 0)
        {
            for (int i = 0; i < dv.Count; i++)
            {
                string sdate = dateFormat(iDateType, dv[i]["date"].ToString());
                if (iDataType == 1)
                {
                    sReturn += "<li><a href='NewsShow.aspx?newsid=" + dv[i]["News_ID"].ToString() + "' title='" + dv[i]["fulltitle"].ToString() + "'>" + dv[i]["title"] + "</a><span class='" + sDateClass + "'>" + sdate + "</span></li>\r\n";
                }
                else
                {
                    sReturn += "<li><a href='ResourceShow.aspx?rsid=" + dv[i]["Resource_ID"].ToString() + "' title='" + dv[i]["fulltitle"].ToString() + "'>" + dv[i]["title"] + "</a><span class='" + sDateClass + "'>" + sdate + "</span></li>\r\n";
                }
            }
        }
        else
        {
            sReturn = "<li>没有找到记录</li>";
        }

        return sReturn;
    }
    /// <summary>
    /// 获得文章列表重载
    /// </summary>
    /// <param name="sMajorID">专业代码</param>
    /// <param name="iOrderType">排序类型,1为时间,2为点击</param>
    /// <param name="iType">分类型,新闻或者资源里面的类型名称</param>
    /// <param name="iCount">读取的记录条数</param>
    /// <param name="iTitleLen">标题长度</param>
    /// <param name="sClickClass">点击数类名</param>
    /// <returns><li><a href="#" title="aaa">sherry</a><span class="clickclass">20</span></li></returns>
    public static string getList(string sMajorID,int iDataType ,int iOrderType, int iType, int iCount, int iTitleLen, string sClickClass)
    {
        DataView dv;
        string sReturn = "";
        dv = getView(sMajorID,iDataType, iOrderType, iType, iCount, iTitleLen);
        if (dv.Count > 0)
        {
            for (int i = 0; i < dv.Count; i++)
            {
                if (iDataType == 1)
                {
                    sReturn += "<li><a href='NewsShow.aspx?newsid=" + dv[i]["News_ID"].ToString() + "' title='" + dv[i]["fulltitle"].ToString() + "'>" + dv[i]["title"] + "</a><span class='" + sClickClass + "'>(" + dv[i]["visit"].ToString() + ")</span></li>\r\n";

                }
                else
                {
                    sReturn += "<li><a href='ResourceShow.aspx?rsid=" + dv[i]["Resource_ID"].ToString() + "' title='" + dv[i]["fulltitle"].ToString() + "'>" + dv[i]["title"] + "</a><span class='" + sClickClass + "'>(" + dv[i]["visit"].ToString() + ")</span></li>\r\n";
                }
            }
        }
        else
        {
            sReturn = "<li>没有找到记录</li>";
        }

        return sReturn;
    }
    /// <summary>
    /// 获得文章列表重载
    /// </summary>
    /// <param name="sMajorID">专业代码</param>
    /// <param name="iOrderType">排序类型,1为时间,2为点击</param>
    /// <param name="iType">分类型,新闻或者资源里面的类型名称</param>
    /// <param name="iCount">读取的记录条数</param>
    /// <param name="iTitleLen">标题长度</param>
    /// <param name="sClickClass">点击数类名</param>
    /// <param name="sDateClass">日期类名</param>
    /// <param name="iDateType">日期类型1: 2009/1/1 ,2:2009-1-1,3:2009年1月1日</param>
    /// <returns><li><a href="#" >sherry</a><span class="clickclass">30</span><span class="dateclass">2009-01-01</span></li></returns>
    public static string getList(string sMajorID, int iDataType,int iOrderType, int iType, int iCount, int iTitleLen, string sClickClass, string sDateClass, int iDateType)
    {
        DataView dv;
        string sReturn = "";
        dv = getView(sMajorID,iDataType, iOrderType, iType, iCount, iTitleLen);
        if (dv.Count > 0)
        {
            for (int i = 0; i < dv.Count; i++)
            {
                string sdate=dateFormat(iDateType,dv[i]["date"].ToString());
                if (iDataType == 1)
                {
                    sReturn += "<li><a href='NewsShow.aspx?newsid=" + dv[i]["News_ID"].ToString() + "' title='" + dv[i]["fulltitle"].ToString() + "'>" + dv[i]["title"] + "</a><span class='" + sClickClass + "'>(" + dv[i]["visit"].ToString() + ")</span>";
                    sReturn += "<span class='" + sDateClass + "'> " + sdate + "</span></li>\r\n";
                }
                else
                {
                    sReturn += "<li><a href='ResourceShow.aspx?rsid=" + dv[i]["Resource_ID"].ToString() + "' title='" + dv[i]["fulltitle"].ToString() + "'>" + dv[i]["title"] + "</a><span class='" + sClickClass + "'>(" + dv[i]["visit"].ToString() + ")</span>";
                    sReturn += "<span class='" + sDateClass + "'> " + sdate + "</span></li>\r\n";
                }
            }
        }
        else
        {
            sReturn = "<li>没有找到记录</li>";
        }

        return sReturn;
    }
    /// <summary>
    /// 获取DataView
    /// </summary>
    /// <param name="sMajorID">专业代码</param>
    /// <param name="iDataType">类型，1为新闻动态，2资源</param>
    /// <param name="iOrderType">排序类型,1为时间,2为点击</param>
    /// <param name="iType">分类型,新闻或者资源里面的类型名称</param>
    /// <param name="iCount">读取的记录条数<</param>
    /// <param name="iTitleLen">标题长度</param>
    /// <returns></returns>
    private static DataView getView(string sMajorID, int iDataType,int iOrderType, int iType, int iCount, int iTitleLen)
    {
        SqlDataSource ds = new SqlDataSource();
        string sOrderStr = string.Empty;
        ds.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Jxzy_Database"].ToString();
        if (iDataType == 1)
        {
            if (iOrderType == 1)
            {
                sOrderStr = "News_ModifyDate";
            }
            else
            {
                sOrderStr = "News_Visit";
            }
            ds.SelectCommand = "SELECT top " + iCount.ToString() + " News_ID,SUBSTRING(News_Title, 1, " + iTitleLen.ToString() + ") AS title, News_Title AS fulltitle, News_Visit as visit,News_ModifyDate as date FROM zyk_News ";
            ds.SelectCommand += " WHERE (News_MajorID = '" + sMajorID + "') AND (News_Type LIKE '%{" + iType.ToString() + "}%') order by " + sOrderStr + " desc";
        }
        else
        {
            if (iOrderType == 1)
            {
                sOrderStr = "Resource_AddDate";
            }
            else
            {
                sOrderStr = "Resource_Visit";
            }
            ds.SelectCommand = "SELECT top " + iCount.ToString() + " Resource_ID,SUBSTRING(Resource_Name, 1, " + iTitleLen.ToString() + ") AS title, Resource_Name AS fulltitle, Resource_Visit as visit,Resource_AddDate as date FROM zyk_Resources ";
            ds.SelectCommand += " WHERE (Resource_MajorID = '" + sMajorID + "') AND (Resource_Type LIKE '%{" + iType.ToString() + "}%') order by " + sOrderStr + " desc";
 
        }

        DataView dv = (DataView)ds.Select(DataSourceSelectArguments.Empty);
        return dv;
    }
    /// <summary>
    /// 根据新闻类型ID获得类型名称
    /// </summary>
    /// <param name="iNewsType">新闻类型ID</param>
    /// <returns>字符串</returns>
    public static string getNewsTypeName( int iNewsType)
    {
        string sReturn=string.Empty;

        SqlDataSource ds = new SqlDataSource();
        ds.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Jxzy_Database"].ToString();
        ds.SelectCommand = "select NewsType_Name from zyk_NewsType where NewsType_ID=@newstype";
        ds.SelectParameters.Add("newstype",DbType.Int32, iNewsType.ToString());
        DataView dv = (DataView)ds.Select(DataSourceSelectArguments.Empty);

        if (dv.Count > 0)
        {
            sReturn = dv[0]["NewsType_Name"].ToString();
        }
        dv.Dispose();
        ds.Dispose();
        return sReturn;
        
    }
    /// <summary>
    /// 获得资源类型名称
    /// </summary>
    /// <param name="iResourceType">类型代码</param>
    /// <returns>名称</returns>
    public static string getResourceTypeName(int iResourceType)
    {
        string sReturn = string.Empty;

        SqlDataSource ds = new SqlDataSource();
        ds.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Jxzy_Database"].ToString();
        ds.SelectCommand = "select ResourceType_Name from zyk_ResourceType where ResourceType_ID=@rstype";
        ds.SelectParameters.Add("rstype", DbType.Int32, iResourceType.ToString());
        DataView dv = (DataView)ds.Select(DataSourceSelectArguments.Empty);

        if (dv.Count > 0)
        {
            sReturn = dv[0]["ResourceType_Name"].ToString();
        }
        dv.Dispose();
        ds.Dispose();
        return sReturn;

    }
    /// <summary>
    /// 获得课程列表
    /// </summary>
    /// <param name="sMajorID">专业ID</param>
    /// <param name="sURL">格式化URL,以$CID$代表课程字段</param>
    /// <returns><li><a href='sURL'>课程名称</a></li></returns>
    public static string getCourseList(string sMajorID,HttpRequest Request)
    {
        string sReturn = string.Empty;
        SqlDataSource ds = new SqlDataSource();
        ds.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Jxzy_Database"].ToString();
        ds.SelectCommand = "select Course_Name,Course_ID from zyk_Courses where Course_MajorID=@majorid";
        ds.SelectParameters.Add("majorid", sMajorID);
        DataView dv = (DataView)ds.Select(DataSourceSelectArguments.Empty);
        //资源类型
        string sType ;
        if (Request.QueryString["rstype"] != null)
            sType = "&rstype=" + Request.QueryString["rstype"].ToString();
        else
            sType = "&rstype=0";
        //当前访问页
        string sPage = Request.ServerVariables["PATH_INFO"].ToString();
        if (dv.Count > 0)
        {
            for (int i = 0; i < dv.Count; i++)
            {
                sReturn += "<li ><a href='" + sPage+"?cid="+dv[i]["Course_ID"].ToString()+sType+ "'>" + dv[i]["Course_Name"].ToString() + "</a></li>";
                
            }
        }
        return sReturn;
    }

}
